package com.madev.isellcrazyitems;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.madev.isellcrazyitems.entity.User;
import com.madev.isellcrazyitems.handler.DatabaseHandler;
import com.madev.sales.R;

public class DashboardActivity extends Activity 
{
	Button btnLogout, btnUpdate, btnPurchase, btnViewReserve;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        /**
         * Dashboard Screen for the application 
         * */        
        // Check login status in database
        
        DatabaseHandler ldb = new DatabaseHandler(getApplicationContext());
        
        
        if(ldb.isUserLoggedIn())
        {
        	User u = ldb.getLogin();
        	Log.d("Customer", u.getUserType());
        	if(u.getUserType().equals("customer"))
        	{
        		setContentView(R.layout.dashboard1);
	        	btnLogout = (Button) findViewById(R.id.btnLogout);
	        	
	        	btnUpdate = (Button) findViewById(R.id.btnUpdateCustomer);
	        	btnViewReserve = (Button) findViewById(R.id.btnViewReserve);
	        	btnPurchase = (Button) findViewById(R.id.btnPurchase);
	        	
	        	btnViewReserve.setOnClickListener(new View.OnClickListener() 
	        	{
	    			
	    			@Override
					public void onClick(View arg0) {
	    				// TODO Auto-generated method stub
	    				Intent reservation = new Intent(getApplicationContext(), ReservationActivity.class);
	    	        	//update.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	        	startActivity(reservation);
	    	        	// Closing dashboard screen
	    	        	//finish();
	    			}
	    		});
	        	
	        	
	        	btnPurchase.setOnClickListener(new View.OnClickListener() 
	        	{
	    			
	    			@Override
					public void onClick(View arg0) {
	    				// TODO Auto-generated method stub
	    				Intent update = new Intent(getApplicationContext(), Purchase.class);
	    	        	//update.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	        	startActivity(update);
	    	        	// Closing dashboard screen
	    	        	//finish();
	    			}
	    		});
	        	
	        	
	        	
	        	btnUpdate.setOnClickListener(new View.OnClickListener() 
	        	{
	    			
	    			@Override
					public void onClick(View arg0) {
	    				// TODO Auto-generated method stub
	    				Intent update = new Intent(getApplicationContext(), UpdateCustomerActivity.class);
	    	        	//update.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	        	startActivity(update);
	    	        	// Closing dashboard screen
	    	        	//finish();
	    			}
	    		});
	        	
	        	
	        	btnLogout.setOnClickListener(new View.OnClickListener() 
	        	{
	    			
	    			@Override
					public void onClick(View arg0) {
	    				// TODO Auto-generated method stub
	    				DatabaseHandler ldb = new DatabaseHandler(getApplicationContext());
	    				ldb.logoutUser();
	    				Intent login = new Intent(getApplicationContext(), LoginActivity.class);
	    	        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    	        	startActivity(login);
	    	        	// Closing dashboard screen
	    	        	finish();
	    			}
	    		});
	        	
	        	
        	}
        	else
        	{
            	// User is Admin
            	Intent admin = new Intent(getApplicationContext(), MainActivity.class);
            	admin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            	startActivity(admin);
            	// Closing dashboard screen
            	finish();
        	}
        	
        }
        else
        {
        	// user is not logged in show login screen
        	Intent login = new Intent(getApplicationContext(), LoginActivity.class);
        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	startActivity(login);
        	// Closing dashboard screen
        	finish();
        }

        
    }
}